package com.lizard.schedule.persistence.sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.lizard.schedule.ScheduleApplication;
import com.lizard.schedule.log.ScheduleLog;
import com.lizard.schedule.manager.AccountManager;
import com.lizard.schedule.net.http.request.bean.ThirdLoginRequest;
import com.lizard.schedule.utils.JniSecurityUtil;

/**
 * 账户相关的SharedPreference
 * 退出登录后将清空数据
 */
public class AccountPref {

    private final String TAG = AccountPref.class.getSimpleName();

    private static AccountPref instance;

    /**
     * pref文件名
     */
    private final static String PREF_NAME = "pref_a";

    /**
     * 最近一次登录的用户名
     */
    private final static String PARAM_USERNAME = "p_a";

    /**
     * 最近一次登录的密码
     */
    private final static String PARAM_PWD = "p_b";

    /**
     * 登录成功返回的token
     */
    private final static String PARAM_TOKEN = "p_c";

    /**
     * 最近一次登录方式
     */
    private final static String PARAM_LOGIN_TYPE = "p_d";

    /**
     * 最近一次的第三方登录方式
     */
    private final static String PARAM_THIRD_LOGIN_TYPE = "P_e";


    private SharedPreferences mPref;

    private AccountPref() {
        mPref = ScheduleApplication.applicationContext
                .getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static AccountPref getInstance() {
        if (instance == null) {
            synchronized (AccountPref.class) {
                if (instance == null) {
                    instance = new AccountPref();
                }
            }
        }
        return instance;
    }

    /**
     *
     * @return
     */
    public int getThirdLoginType(){
        return mPref.getInt(PARAM_THIRD_LOGIN_TYPE, 0);
    }

    /**
     *
     * @param thirdLogiType
     */
    public void saveThirdLoginType(int thirdLogiType){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(PARAM_THIRD_LOGIN_TYPE, thirdLogiType);
        editor.commit();
    }

    /**
     * 记录最近一次登录方式
     *
     * @param loginType
     */
    public void saveLoginType(int loginType){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(PARAM_LOGIN_TYPE, loginType);
        editor.commit();
    }

    /**
     * 获取最近一次登录方式
     *
     * @return
     */
    public int getLoginType(){
        return mPref.getInt(PARAM_LOGIN_TYPE, AccountManager.LOGIN_TYPE_NO_RECORD);
    }

    /**
     * 记录最近一次登录用户名、密码
     *
     * @param username
     * @param pwd
     * @param token
     */
    public void saveLoginAccount(String username, String pwd, String token){
        String encryptUsername = null;
        String encryptPwd = null;
        String encryptToken = null;

        if(!TextUtils.isEmpty(username)){
            try {
                encryptUsername = JniSecurityUtil.aesEncrypt(username);
            } catch (Exception e) {}
        }

        if(!TextUtils.isEmpty(pwd)){
            try {
                encryptPwd = JniSecurityUtil.aesEncrypt(pwd);
            } catch (Exception e) {}
        }

        if(!TextUtils.isEmpty(token)){
            try {
                encryptToken = JniSecurityUtil.aesEncrypt(token);
            } catch (Exception e) {}
        }

        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(PARAM_USERNAME, encryptUsername);
        editor.putString(PARAM_PWD, encryptPwd);
        editor.putString(PARAM_TOKEN, encryptToken);
        editor.commit();
    }

    /**
     * 获取最近一次登录用户名
     *
     * @return
     */
    public String getUsername(){
        String encryptUsername = mPref.getString(PARAM_USERNAME, null);
        String decryptUsername = null;
        if(!TextUtils.isEmpty(encryptUsername)){
            try {
                decryptUsername = JniSecurityUtil.aesDecrypt(encryptUsername);
            } catch (Exception e) {}
        }
        return decryptUsername;
    }

    /**
     * 获取最近一次登录密码
     *
     * @return
     */
    public String getPwd(){
        String encryptPwd = mPref.getString(PARAM_PWD, null);
        String decryptPwd = null;
        if(!TextUtils.isEmpty(encryptPwd)){
            try {
                decryptPwd = JniSecurityUtil.aesDecrypt(encryptPwd);
            } catch (Exception e) {}
        }
        return decryptPwd;
    }

    /**
     * 获取最近一次登录的token
     *
     * @return
     */
    public String getToken(){
        String encryptToken = mPref.getString(PARAM_TOKEN, null);
        String decryptToken = null;
        if(!TextUtils.isEmpty(encryptToken)){
            try {
                decryptToken = JniSecurityUtil.aesDecrypt(encryptToken);
            } catch (Exception e) {}
        }
        return decryptToken;
    }

    /**
     * 清空数据
     */
    public void clear(){
        SharedPreferences.Editor editor = mPref.edit();
        editor.clear();
        editor.commit();
    }

}
